import Storage from './util/storage'

const NotLoginAuth = [
    '/login',
]

let LoginInfo = {
    logined : false,
    username : null,
}

export const Login = (userInfo) =>{
    LoginInfo = {
        ...userInfo,
        logined : true,
        lastime : new Date().getTime(),
    }
    Storage.setStore('UserInfo', LoginInfo)
}

export const LoginOut = () =>{
    LoginInfo = {
        logined : false,
    }
    Storage.removeStore('UserInfo')
}

export const getUser = () =>{ return LoginInfo }


let storeUser = Storage.getStore('UserInfo')
if(storeUser && storeUser.logined){ Login(storeUser) }

export default (nextState, replace) => {
    let path = nextState.location.pathname.toLocaleLowerCase()
    if(path == '/loginout'){
        LoginOut()
        replace('/login')
    }
    if(NotLoginAuth.indexOf(path) == -1 && LoginInfo.logined == false){
        replace('/login')
    }
}